<!doctype html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>学校固定资产管理系统</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/login.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <link rel="stylesheet" href="./lib/layui/css/modules/layer/default/layer.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script src="./js/MD5.js" charset="utf-8"></script>
    <script src="lib/layui/lay/modules/layer.js" charset="utf-8"></script>
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body class="login-bg">

    <div class="login layui-anim layui-anim-up">
        <div class="message">固定资产管理系统登录</div>
        <div id="darkbannerwrap"></div>

        <form method="post" class="layui-form">
            <input name="Phone" id="Phone" placeholder="手机号" type="text" lay-verify="required|phone" class="layui-input">
            <hr class="hr15">
            <input name="Password" id="Password" lay-verify="required" placeholder="密码" type="password" class="layui-input">
            <hr class="hr15">
            <input value="登录" lay-submit lay-filter="login" style="width:100%;" type="submit">
            <hr class="hr20">
        </form>
    </div>

    <script>
        function parseJwt(token) {
            var base64Url = token.split('.')[1];
            var base64 = base64Url.replace('-', '+').replace('_', '/');
            var user = JSON.parse(b64_to_utf8(base64));
            sessionStorage.setItem("UserName", user.username);
        };
        function b64_to_utf8(str) {
            return decodeURIComponent(escape(window.atob(str)));
        }
        $(function () {
            sessionStorage.setItem("UserName", null);
            sessionStorage.setItem("api", "http://localhost:9991/api/");
            var url = sessionStorage.getItem("api");
            layui.use('form', function () {
                var form = layui.form;

                // 密码验证
                form.verify({
                    Phone: [
                        /^[\S]{6,12}$/
                        , '密码必须6到12位，且不能出现空格'
                    ]
                    , equal: function (value, item) { //value：表单的值、item：表单的DOM对象
                        if (value != layui.$('input[name="newPassWord"]').val()) {
                            return '密码不相同请重新输入！'
                        }
                    }, password: [
                        /^[\S]{6,12}$/
                        , '密码必须6到12位，且不能出现空格'
                    ]
                })



                form.on('submit(login)', function (data) {
                    layer.load();
                    var phone = $("#Phone").val();
                    var password = $("#Password").val();
                    $.ajax({
                        type: "POST",
                        timeout: 5000,
                        url: sessionStorage.getItem("api") + "users/token",
                        data: {
                            Phone: phone,
                            PassWord: md5(password)
                        },
                        contentType: "application/x-www-form-urlencoded",
                        dataType: "Json",
                        success: function (res) {
                            if (res.Code == 200) {
                                sessionStorage.setItem("token", res.Data);
                                parseJwt(res.Data);
                                layer.closeAll('loading');
                                layer.msg('登录成功', {
                                    time: 1500
                                }, function () {
                                    location.href = 'index.html';
                                })
                            } else {
                                layer.closeAll('loading');
                                layer.msg(res.ErrorMessage);
                            }
                        },
                        complete: function (XMLHttpRequest, status) {
                            if (status == 'timeout') {
                                layer.closeAll('loading');
                                layer.msg("登陆超时，请稍后重试");
                            }
                        }
                    })
                    return false;
                });
            });
        })
    </script>
    <!-- 底部结束 -->
</body>

</html>